package designPattern.singleton;

public class SingletonTest5 {
    public static void main(String[] args) {
        Singleton5 instance1 = Singleton5.getInstance();
        Singleton5 instance2 = Singleton5.getInstance();

        //判断两个对象是否相同
        System.out.println(instance2==instance1);

        System.out.println("instance1.hashCode() = " + instance1.hashCode());
        System.out.println("instance2.hashCode() = " + instance2.hashCode());
    }
}

//饿汉式（双重检查）

class Singleton5{
    private static Singleton5 instance;

    private Singleton5(){}

    //公共静态方法，双重检查，解决线程安全问题与懒加载问题，性能优秀
    public static Singleton5 getInstance(){
        if(instance==null){
            synchronized (Singleton5.class) {
                if(instance==null) {
                    instance = new Singleton5();
                }
            }
        }

        return instance;
    }
}